package isp.apps.example.shoppingadmin.action;

import com.opensymphony.xwork.ActionSupport;

import isp.apps.example.shoppingcart.persistence.CompletedOrderDirectory;
import isp.apps.example.shoppingcart.persistence.factory.CompletedOrderPersistenceFactory;

import java.util.List;

public class GetCompletedOrdersAction extends ActionSupport
{
  private CompletedOrderDirectory myOrderDir = CompletedOrderPersistenceFactory
    .getCompletedOrderDirectory();
  private List orderList;
  private List incompleteOrders;
  private int page = 1;
  private Integer previousPage;
  private Integer nextPage;
  private int orderCount = 0;
  private int pageCount =1;
  private static final int ORDERS_PER_PAGE = 10;
  private int firstOrder = 0;
  private int lastOrder = 0;
  
  public GetCompletedOrdersAction()
  {
  }
  
  public void setPage(int pPage)
  {
     page = pPage;
  }
  
  public String execute()
  {
    List tempList = myOrderDir.getOrders();
    incompleteOrders = myOrderDir.getIncompleteOrders();
    if (tempList== null)
       return SUCCESS;
    orderCount = tempList.size();
    if (orderCount == 0)
      return SUCCESS;
    pageCount = orderCount / ORDERS_PER_PAGE;
    validate();
    // theres only one page
    if ((page == pageCount) && (page ==1))
    {
       orderList=tempList.subList(0,orderCount);
       firstOrder = 1;
       lastOrder = orderCount;
    }
    // more than one page and we're looking at the last one
    else if (page == pageCount)
    {
       orderList=tempList.subList((page-1)*ORDERS_PER_PAGE,orderCount);
       previousPage = new Integer (page - 1);
       firstOrder = (page-1)*ORDERS_PER_PAGE+1;
       lastOrder = orderCount;
    }
    // more than one page and we're on the first one
    else if (page ==1)
    {
       orderList = tempList.subList(0,ORDERS_PER_PAGE);
       nextPage = new Integer(2);
       firstOrder=1;
       lastOrder=ORDERS_PER_PAGE;
    }
    // more than one page and we're on neither the first or last
    else
    {
      orderList = tempList.subList((page-1)*ORDERS_PER_PAGE,page*ORDERS_PER_PAGE);
      nextPage = new Integer(page+1);
      previousPage = new Integer(page-1);
      firstOrder=(page-1)*ORDERS_PER_PAGE+1;
      lastOrder=page*ORDERS_PER_PAGE;
    }
    return SUCCESS;
  }
  
  public void validate()
  {
    if (orderCount % ORDERS_PER_PAGE > 0)
       pageCount++;
    if (page > pageCount)
       page = pageCount;
    if (page < 1)
       page = 1;
  }

  public void setOrderList(List orderList)
  {
    this.orderList = orderList;
  }

  public List getOrderList()
  {
    return orderList;
  }

  public Integer getPreviousPage()
  {
    return previousPage;
  }

  public Integer getNextPage()
  {
    return nextPage;
  }

  public int getPage()
  {
    return page;
  }

  public void setOrderCount(int orderCount)
  {
    this.orderCount = orderCount;
  }

  public int getOrderCount()
  {
    return orderCount;
  }

  public int getPageCount()
  {
    return pageCount;
  }

  public int getFirstOrder()
  {
    return firstOrder;
  }

  public int getLastOrder()
  {
    return lastOrder;
  }

  public void setIncompleteOrders(List incompleteOrders)
  {
    this.incompleteOrders = incompleteOrders;
  }

  public List getIncompleteOrders()
  {
    return incompleteOrders;
  }
}
